package cn.cdqf.web;

import cn.cdqf.bean.TClass;
import cn.cdqf.common.ResultResponse;
import cn.cdqf.service.ClassService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ClassServlet {

    @Autowired
    private ClassService classService;

    @RequestMapping("/getClassName")
    @ResponseBody
    public String getClassName(){
        List<TClass> className = classService.getClassName();
        return JSON.toJSONString(className);
    }


    @RequestMapping("/queryClass")
    @ResponseBody
    public List<TClass> queryClass(){
        List<TClass> classList = classService.queryClass();
        return classList;
    }

    @RequestMapping("/deleteClass")
    @ResponseBody
    public ResultResponse deleteClass(Integer id){
        int i = classService.deleteClass(id);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @RequestMapping("/queryClassById")
    @ResponseBody
    public TClass queryClassById(Integer id){
        TClass tClass = classService.queryClassById(id);
        return tClass;
    }


    @RequestMapping("/updateClass/{id}")
    @ResponseBody
    public ResultResponse updateClass(TClass tClass, @PathVariable("id") int id){
        int i = classService.updateClass(tClass,id);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @RequestMapping("/addClass")
    @ResponseBody
    public ResultResponse addClass(TClass tClass){
        System.out.println(tClass);
        int i = classService.addClass(tClass);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }
}
